//
//  RootViewController.m
//  WisdomZhuHai
//
//  Created by yewei on 13-10-14.
//  Copyright (c) 2013年 wyd. All rights reserved.
//

#import "RootViewController.h"
#import "TabBarController.h"
#import "AppDelegate.h"
#import "Common.h"
#import "ProtocolManager.h"
#import "GuideViewController.h"
#import "newSideBarViewController.h"
#import "TransforController.h"
#import "GuidePageViewController.h"
#import "EmptyViewController.h"

@interface RootViewController ()

@property (nonatomic,strong) TabBarController *tabbarControl;

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)dealloc
{
    [_tabbarControl release];
    [super dealloc];
}

-(void)loadView
{
    self.view = [[[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds]autorelease];
}

-(void)showMainView
{
    
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [self.view removeFromSuperview];
//    self.tabbarControl = [[[TabBarController alloc] init] autorelease];
    TabBarController *tabbarControl = [[[TabBarController alloc] init]autorelease];
    self.tabbarControl = tabbarControl;
    UIView* bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tabbarControl.tabBar.bounds.size.width, self.tabbarControl.tabBar.bounds.size.height)];
    bgView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
    if([Common getIosVersion] >= 7.0)
    {
        [self.tabbarControl.tabBar insertSubview:bgView atIndex:0];
    }
    else
    {
        [self.tabbarControl.tabBar insertSubview:bgView atIndex:1];
    }
    [bgView release];
    
//     newSideBarViewController *menuController = [[[newSideBarViewController alloc] init] autorelease];
//    IQSideMenuController *sideMenuController = [[IQSideMenuController alloc] initWithMenuViewController:menuController andContentViewController:tabbarControl];
//    IQSideMenuController *sideMenuController = [TransforController initIQSideMenuControllerWithMenuViewController:menuController andContentViewController:tabbarControl];
//    self.sideMenuController = sideMenuController;
//    menuController.closeDelegete = self;
//    tabbarControl.closeDetegate = self;
//    [appDelegate.window setRootViewController:sideMenuController];
    [appDelegate.window setRootViewController:self.tabbarControl];
}

- (void)showGuideView
{
    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [self.view removeFromSuperview];
    
//    GuideViewController *guideViewCtrl = [[GuideViewController alloc] initWithFrame:self.view.bounds];
//    [appDelegate.window setRootViewController:guideViewCtrl];
//    [guideViewCtrl release];
    
    NSArray* imgs = @[[UIImage imageNamed:@"Guide/welcome_1.jpg"], [UIImage imageNamed:@"Guide/welcome_2.jpg"], [UIImage imageNamed:@"Guide/welcome_3.jpg"]];
    EmptyViewController *guideViewCtrl = [[EmptyViewController alloc] initWithFrame:self.view.bounds];
    GuidePageViewController *instance_guideViewCtrl = [GuidePageViewController getInstance:self.view.bounds];
    [guideViewCtrl.view addSubview:instance_guideViewCtrl];
    NSMutableArray *array_temp = [[NSMutableArray alloc]init];
    for(int i = 0; i < [imgs count]; i ++){
        UIViewController *instance_view_temp = [[UIViewController alloc]init];
        [instance_view_temp.view setFrame:self.view.bounds];
        UIImageView *img_temp = [[UIImageView alloc]initWithImage:imgs[i]];
        [img_temp setFrame:instance_view_temp.view.frame];
        [instance_view_temp.view addSubview:img_temp];
        [array_temp addObject:instance_view_temp];
    }
    instance_guideViewCtrl.viewPageArray = [NSArray arrayWithArray:array_temp];
    [instance_guideViewCtrl initWithViews];
//    guideViewCtrl.hidesBottomBarWhenPushed = YES;
    [appDelegate.window setRootViewController:guideViewCtrl];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];

    id firstOpenFlag = [[NSUserDefaults standardUserDefaults] objectForKey:@"firstOpenFlag"];
    if (firstOpenFlag == nil) {
        [self showGuideView];
        return;
    }
    
    UIImage * img = nil;
	// Do any additional setup after loading the view.
    img = [UIImage imageNamed:@"Fixedpage/splash.jpg"];
//    if(fabsf(self.view.bounds.size.height - 568) < 0.001)
//    {
//        img = [UIImage imageNamed:@"common/splash-568h.png"];
//    }
//    else
//    {
//        img = [UIImage imageNamed:@"common/splash.png"];
//    }
    UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
//    imgView.contentMode = UIViewContentModeCenter;
    [imgView setFrame:CGRectMake(0, 0, 320, 568)];
    [self.view addSubview:imgView];
    imgView.alpha = 0.2;
    [UIView animateWithDuration:3.0f animations:^{
        imgView.alpha = 1.0f;
//        CGAffineTransform newTransform =  CGAffineTransformScale(imgView.transform, 1.3, 1.3);
//        [self.view setTransform:newTransform];
    } completion:^(BOOL finished){
        [self showMainView];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)pushViewController:(UIViewController*)viewController animated:(BOOL)animated
{
 //   [self.sideViewController presentViewController:viewController animated:animated completion:nil];
//    [self.sideViewController hideSideViewController:NO];
    UIViewController *ctl = self.tabbarControl;
    
    if([ctl isKindOfClass:[UITabBarController class]])
    {
        UIViewController *selectCtrl = ((UITabBarController *)ctl).selectedViewController;
        if([selectCtrl isKindOfClass:[UINavigationController class]])
        {
            [((UINavigationController*)selectCtrl) pushViewController:viewController animated:YES];
        }
        else
        {
            [selectCtrl.navigationController pushViewController:viewController animated:YES];
        }
    }
    else if([ctl isKindOfClass:[UINavigationController class]])
    {
        [((UINavigationController *)ctl) pushViewController:viewController animated:YES];
    }
}

- (void)popViewController:(UIViewController*)viewController animated:(BOOL)animated
{
    [viewController dismissViewControllerAnimated:YES completion:nil];
}

@end
